<?php

namespace app\common\service;

use app\common\Constants;
use app\common\exception\BusinessException;

class PrincipalService
{
    public static function setUserInfo(array $data)
    {
        return request()->userInfo = $data;
    }

    public static function getUserInfo()
    {
        if(request()->userInfo){
            return request()->userInfo;
        } else {
            throw new BusinessException(Constants::E_COMMON_UNAUTHENTICATED);
        }
    }

    public static function getUserId()
    {
        if(request()->userInfo){
            return request()->userInfo['id'];
        } else {
            throw new BusinessException(Constants::E_COMMON_UNAUTHENTICATED);
        }
    }

    public static function getUserName()
    {
        if(request()->userInfo){
            return request()->userInfo['username'];
        } else {
            throw new BusinessException(Constants::E_COMMON_UNAUTHENTICATED);
        }
    }



    public static function getDeptId()
    {
        if(request()->userInfo){
            return request()->userInfo['deptId'];
        } else {
            throw new BusinessException(Constants::E_COMMON_UNAUTHENTICATED);
        }
    }


    public static function getPerms()
    {
        if(request()->userInfo){
            return request()->userInfo['perms'];
        } else {
            throw new BusinessException(Constants::E_COMMON_UNAUTHENTICATED);
        }
    }

//    public static function getOriginPerms()
//    {
//        return request()->userInfo['origin'];
//    }

    public static function hasLogin()
    {
        return request()->userInfo;
    }

    public static function isSuperUser()
    {
        if(request()->userInfo){
            $userInfo = request()->userInfo;
            return $userInfo['id'] === Constants::SUPER_USER_ID;
        } else {
            throw new BusinessException(Constants::E_COMMON_UNAUTHENTICATED);
        }
    }
}